package com.jfinal.weixin.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.entity.Movie;
import org.jsoup.Jsoup;

import java.util.ArrayList;

public class DianYinUtil {
    //获取电影列表信息修改版
    public static String searchDianYinGetStr(String searchName) {
        String movie_str = "";
        try {
            int pg = 1;
            int pageCount=0;
            do{
                String search_url = "http://api.tiankongapi.com/api.php/provide/vod/?ac=list&wd="+searchName+"&pg="+pg;
                String str = Jsoup.connect(search_url).ignoreContentType(true).get().body().text();
                JSONObject jsonObject = JSONObject.parseObject(str);
                //Integer total = jsonObject.getInteger("total");
                pageCount = jsonObject.getInteger("pagecount");
                JSONArray jsonArray = jsonObject.getJSONArray("list");
                if (jsonArray.size()!=0){
                    for (int i=0;i<jsonArray.size();i++){

                        JSONObject dy_json = jsonArray.getJSONObject(i);
                        Movie movie = new Movie(dy_json.getString("vod_id"), dy_json.getString("vod_name"), dy_json.getString("vod_play_url"));
                        movie_str += "id\t" + movie.getVod_id() + ",\t" + movie.getVod_name() +"\n";
                        if(i==jsonArray.size()-1){
                            movie_str +="\n请输入点播看+电影名前对应的编号\n示例：\t点播看10086";
                        }
                    }
                }else {
                    return "";
                }
                //System.out.println(jsonArray);
                //翻页
                pg++;
            }while (pg<=pageCount);
        }catch (Exception e){
            e.printStackTrace();
        }
        return movie_str;
    }


    public static Movie getMovieDetails(String vod_id){
        //建议写成通用的能解析所有json数据的方法
        Movie movie = new Movie();
        try {
                String search_url = "http://api.tiankongapi.com/api.php/provide/vod/?ac=detail&ids="+vod_id;
                String str = Jsoup.connect(search_url).ignoreContentType(true).get().body().text();
                JSONObject jsonObject = JSONObject.parseObject(str);
                Integer total = jsonObject.getInteger("total");
                if(total>0){
                    JSONObject dy_json = jsonObject.getJSONArray("list").getJSONObject(0);
                     movie = new Movie(dy_json.getString("vod_id"),
                         dy_json.getString("vod_name"),
                         dy_json.getString("vod_play_url"),
                         dy_json.getString("vod_content"),
                         dy_json.getString("vod_class"),
                         dy_json.getString("vod_area"),
                         dy_json.getString("vod_year"),
                         dy_json.getString("vod_sub"),
                         dy_json.getString("vod_actor"),
                         dy_json.getString("vod_director"),
                         dy_json.getString("vod_time"),
                         dy_json.getString("vod_remarks"),
                         dy_json.getString("vod_douban_score"),
                         dy_json.getString("vod_pic")
                         );
                }
        }catch (Exception e){
            e.printStackTrace();
        }
        return movie;
    }

    //废弃的代码块
    /*    public static ArrayList searchDianYin(String searchName) {
        ArrayList list = new ArrayList();
        try {
            int pg = 1;
            int pageCount=0;
            do{
                String search_url = "http://api.tiankongapi.com/api.php/provide/vod/?ac=list&wd="+searchName+"&pg="+pg;
                String str = Jsoup.connect(search_url).ignoreContentType(true).get().body().text();
                JSONObject jsonObject = JSONObject.parseObject(str);
                //Integer total = jsonObject.getInteger("total");
                pageCount = jsonObject.getInteger("pagecount");
                JSONArray jsonArray = jsonObject.getJSONArray("list");
                for (int i=0;i<jsonArray.size();i++){
                    JSONObject dy_json = jsonArray.getJSONObject(i);
                    Movie movie = new Movie(dy_json.getString("vod_id"), dy_json.getString("vod_name"), dy_json.getString("vod_play_url"));
                    list.add(movie);
                }
                //翻页
                pg++;
            }while (pg<=pageCount);

        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }*/
}
